package com.ruoyi.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.ZhwyAdvancedeposit;
import com.ruoyi.domain.ZhwyChargedata;
import com.ruoyi.domain.ZhwyChargeitem;
import com.ruoyi.domain.ZhwyRentalcontract;
import com.ruoyi.dto.PendingPayment;
import com.ruoyi.mapper.ZhwyChargedataMapper;
import com.ruoyi.mapper.ZhwyChargeitemMapper;
import com.ruoyi.mapper.ZhwyMeterreadingdataMapper;
import com.ruoyi.mapper.ZhwyRentalcontractMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName AmountMoney
 * @Description TODO
 * @Author liuzhiqiang
 * @Date 2024年05月10日 21:39
 * @Version 1.0
 */
@RestController
@RequestMapping("/AmountMoney")
public class AmountMoney {
    @Autowired
    private ZhwyRentalcontractMapper zhwyRentalcontractMapper;
    @Autowired
    private ZhwyChargeitemMapper zhwyChargeitemMapper;
    @Autowired
    private ZhwyChargedataMapper zhwyChargedataMapper;
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:query')")
    @GetMapping("/Pending/{id}")//代缴费金额
    public AjaxResult Pending(@PathVariable("id") Long id)
    {   //根据userid查询合同
        List<ZhwyRentalcontract> zhwyRentalcontracts = zhwyRentalcontractMapper.selectZhwyRentalcontractByhirerId(id);
        //根据合同查商铺
        List list=new ArrayList();
        for(ZhwyRentalcontract ren:zhwyRentalcontracts){
            Long shopsId = ren.getShopsId();
            List<ZhwyChargedata> zhwyChargedata = zhwyChargedataMapper.selectZhwyChargedataByShopId1(shopsId);
            for(ZhwyChargedata data:zhwyChargedata){
                PendingPayment pp=new PendingPayment();
                pp.setStartTime(data.getStartTime());//开始时间
                pp.setEndTime(data.getEndTime());//结束时间
                BigDecimal money = data.getMoney();//金额
                pp.setMoney(money);
                BigDecimal fundsReceived = data.getFundsReceived();//实收金额
                pp.setFundsReceived(fundsReceived);
                if(fundsReceived ==null){
                    fundsReceived = BigDecimal.valueOf(0);
                }
                BigDecimal subtract = money.subtract(fundsReceived);//代缴费金额
                if(subtract !=null){pp.setSubtract(subtract);}

                String billNumber = data.getBillNumber();//单号
                pp.setBillNumber(billNumber);
                Long chargeitemId = data.getChargeitemId();
                //收费项目名
                String name = zhwyChargeitemMapper.selectZhwyChargeitemById(chargeitemId).getName();
                pp.setName(name);
                list.add(pp);
            }
        }
        return AjaxResult.success(list);
    }
    @PreAuthorize("@ss.hasPermi('feeDeposit:advancedeposit:query')")
    @GetMapping("/Pending1/{id}")//历史缴费金额
    public AjaxResult Pending1(@PathVariable("id") Long id)
    {   //根据userid查询合同
        List<ZhwyRentalcontract> zhwyRentalcontracts = zhwyRentalcontractMapper.selectZhwyRentalcontractByhirerId(id);
        //根据合同查商铺
        List list=new ArrayList();
        for(ZhwyRentalcontract ren:zhwyRentalcontracts){
            Long shopsId = ren.getShopsId();
            List<ZhwyChargedata> zhwyChargedata = zhwyChargedataMapper.selectZhwyChargedataByShopId(shopsId);
            for(ZhwyChargedata data:zhwyChargedata){
                PendingPayment pp=new PendingPayment();
                pp.setStartTime(data.getStartTime());//开始时间
                pp.setEndTime(data.getEndTime());//结束时间
                BigDecimal money = data.getMoney();//金额
                pp.setMoney(money);
                BigDecimal fundsReceived = data.getFundsReceived();//实收金额
                pp.setFundsReceived(fundsReceived);
                if(fundsReceived ==null){
                    fundsReceived = BigDecimal.valueOf(0);
                }
                BigDecimal subtract = money.subtract(fundsReceived);//代缴费金额
                if(subtract !=null){pp.setSubtract(subtract);}
                String billNumber = data.getBillNumber();//单号
                pp.setBillNumber(billNumber);
                Long chargeitemId = data.getChargeitemId();
                //收费项目名
                String name = zhwyChargeitemMapper.selectZhwyChargeitemById(chargeitemId).getName();
                pp.setName(name);
                list.add(pp);
            }
        }
        return AjaxResult.success(list);
    }
}
